<script setup lang="ts">
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import { PureTableBar } from "@/components/RePureTableBar";
import { hasAuth } from "@/router/utils";
import { ref } from "vue";
import { useUser } from "./utils/hook";

import Delete from "@iconify-icons/ep/delete";
import EditPen from "@iconify-icons/ep/edit-pen";
import Refresh from "@iconify-icons/ep/refresh";
import AddFill from "@iconify-icons/ri/add-circle-line";
import SearchIcon from "@iconify-icons/ri/search-line";

defineOptions({
  name: "AchievTrans"
});

const formRef = ref();
const tableRef = ref();

const {
  form,
  tableCol,
  loading,
  columns,
  dataList,
  pagination,
  deviceDetection,
  onSearch,
  resetForm,
  openDialog,
  openDetail,
  handleDelete,
  handleSizeChange,
  handleCurrentChange,
  exportModuleData
} = useUser(tableRef);
</script>

<template>
  <div :class="['flex', 'justify-between', deviceDetection() && 'flex-wrap']">
    <div class="w-full">
      <el-form
        ref="formRef"
        :inline="true"
        :model="form"
        class="search-form bg-bg_color w-[99/100] pl-8 pt-[12px] overflow-auto"
      >
        <el-form-item label="专利名称" prop="patentName">
          <el-input
            v-model="form.patentName"
            placeholder="请输入"
            clearable
            class="!w-[250px]"
          />
        </el-form-item>
        <el-form-item label="专利类型" prop="patentType">
          <el-select
            v-model="form.patentType"
            placeholder="请选择"
            clearable
            class="!w-[200px]"
          >
            <el-option label="发明专利" value="发明专利" />
            <el-option label="实用新型专利" value="实用新型专利" />
            <el-option label="外观专利" value="外观专利" />
          </el-select>
        </el-form-item>
        <el-form-item label="审核状态" prop="examineStatus">
          <el-select
            v-model="form.examineStatus"
            placeholder="请选择"
            clearable
            class="!w-[180px]"
          >
            <el-option label="审核中" :value="0" />
            <el-option label="已审核" :value="1" />
            <el-option label="已驳回" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select
            v-model="form.status"
            placeholder="请选择"
            clearable
            class="!w-[180px]"
          >
            <el-option label="进行中" :value="1" />
            <el-option label="已完成" :value="2" />
          </el-select>
        </el-form-item>

        <el-form-item label="第一发明人类型" prop="firstInventorType">
          <el-select
            v-model="form.firstInventorType"
            placeholder="请选择"
            clearable
            class="!w-[180px]"
          >
            <el-option label="学生" value="学生" />
            <el-option label="院内老师" value="院内老师" />
            <el-option label="其他人员" value="其他人员" />
          </el-select>
        </el-form-item>
        <el-form-item label="第一发明人" prop="firstInventor">
          <el-input
            v-model="form.firstInventor"
            placeholder="请输入"
            class="!w-[180px]"
          />
        </el-form-item>
        <el-form-item label="转让合同签订日期" prop="searchDate">
          <el-date-picker
            v-model="form.searchDate"
            class="!w-[250px]"
            type="daterange"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            :icon="useRenderIcon('ri:search-line')"
            :loading="loading"
            @click="onSearch"
          >
            搜索
          </el-button>
          <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
            重置
          </el-button>
        </el-form-item>
      </el-form>

      <PureTableBar
        v-if="tableCol.load"
        title=""
        :tableCol="tableCol"
        :columns="columns"
        @refresh="onSearch"
      >
        <template #buttons>
          <el-button
            v-if="hasAuth('achievTrans:btn:add')"
            type="primary"
            :icon="useRenderIcon(AddFill)"
            @click="openDialog('新增')"
          >
            新增
          </el-button>
          <el-button
            v-if="hasAuth('achievTrans:btn:export')"
            type="success"
            @click="exportModuleData"
          >
            导出
          </el-button>
        </template>
        <template v-slot="{ size, dynamicColumns }">
          <pure-table
            ref="tableRef"
            row-key="id"
            adaptive
            :adaptiveConfig="{ offsetBottom: 90 }"
            align-whole="center"
            :loading="loading"
            :size="size"
            :data="dataList"
            :columns="dynamicColumns"
            :pagination="{ ...pagination, size }"
            :header-cell-style="{
              background: 'var(--el-fill-color-light)',
              color: 'var(--el-text-color-primary)'
            }"
            @page-size-change="handleSizeChange"
            @page-current-change="handleCurrentChange"
          >
            <template #operation="{ row }">
              <el-button
                class="reset-margin"
                link
                type="primary"
                :size="size"
                :icon="useRenderIcon(SearchIcon)"
                @click="openDetail('查看', row)"
              >
                查看
              </el-button>
              <el-button
                v-if="hasAuth('achievTrans:btn:up')"
                class="reset-margin !ml-[5px]"
                link
                type="primary"
                :size="size"
                :icon="useRenderIcon(EditPen)"
                @click="openDialog('修改', row)"
              >
                修改
              </el-button>
              <template
                v-if="row.examineStatus != 1 && hasAuth('achievTrans:btn:del')"
              >
                <el-popconfirm
                  title="是否确认删除?"
                  @confirm="handleDelete(row)"
                >
                  <template #reference>
                    <el-button
                      class="reset-margin !ml-[5px]"
                      link
                      type="danger"
                      :size="size"
                      :icon="useRenderIcon(Delete)"
                    >
                      删除
                    </el-button>
                  </template>
                </el-popconfirm>
              </template>
            </template>
          </pure-table>
        </template>
      </PureTableBar>
    </div>
  </div>
</template>

<style scoped lang="scss">
.search-form {
  :deep(.el-form-item) {
    margin-bottom: 12px;
  }
}
</style>
